<?php
namespace app\admin\controller\Sys\middleware;
use app\admin\controller\Admin;
use think\exception\ValidateException;

class updateAdminConfig extends Admin
{
	
    public function handle($request, \Closure $next)
    {
        $data = $request->param();

        if($data['domain']){
            $domain = db('admin_config')->where(['name'=>'domain'])->findOrEmpty();
            if(empty($domain)){
                throw new ValidateException ('基本配置中必须有绑定域名设置！');
            }
            $where = "filepath like '" . $domain['data'] ."%'";
            $count = db('admin_file')->whereRaw($where)->count();
            if($count>0){
                db('admin_file')->where($where)
                    ->exp('filepath',"REPLACE(filepath,'".$domain['data']."','".$data['domain']."')")
                    ->update();
            }
        }

		return $next($request);
    }
}